找传奇、传世资源到传世资源站!

C# 示波器 示例源码下载

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboardusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO.Ports;using System.Windows.Forms.DataVisualization.Charting;namespace Oscilloscope{ public partial class Oscilloscope : Form { #region 变量定义 //ConfigPrama configPramaClass;// = new ConfigPrama(); public Int32 recFrameNum = 0; //接收到的数据帧数 public Int32 recByteNum = 0; //接收到的数据字节数 private List<byte> recBuffer = new List<byte>(); private bool handerListening = false; private bool comClosing = false; private int pointIndex = 0; private bool axisChange = false; #endregion public Oscilloscope() { InitializeComponent(); } #region 串口初始化 private void buttonSwitch_Click(object sender, EventArgs e) { try { if (serialPort1.IsOpen) { comClosing = true;//串口关闭动作正在进行 while (handerListening) Application.DoEvents();//关闭串口前处理完消息队列中的所有事件 serialPort1.Close(); if (!serialPort1.IsOpen) { buttonSwitch.Text = "打开串口"; labelStatus.ForeColor = Color.Black; } } else { comClosing = false; serialPort1.PortName = comboBoxPort.Text; serialPort1.BaudRate = Convert.ToInt32(comboBoxBaudRate.Text); serialPort1.DataBits = Convert.ToInt32(comboBoxData.Text); switch (comboBoxCheck.Text) { case "None": serialPort1.Parity = System.IO.Ports.Parity.None; break; case "Odd": serialPort1.Parity = System.IO.Ports.Parity.Odd; break; case "Even": serialPort1.Parity = System.IO.Ports.Parity.Even; break; case "Mark": serialPort1.Parity = System.IO.Ports.Parity.Mark; break; case "Space": serialPort1.Parity = System.IO.Ports.Parity.Space; break; default: break; } serialPort1.StopBits = (System.IO.Ports.StopBits)Convert.ToInt32(comboBoxStopBit.Text); try { serialPort1.Open(); } catch(Exception ex) { //现实异常信息给客户。 MessageBox.Show(ex.Message); //捕获到异常信息,创建一个新的comm对象,之前的不能用了。 //serialPort1 = new SerialPort(); } if (serialPort1.IsOpen) { buttonSwitch.Text = "关闭串口"; labelStatus.ForeColor = Color.Red; } } } catch (Exception err) { MessageBox.Show(err.Message); } } #endregion #region 串口接收 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { //struct _axis //{ double xValue; double yValue = 0; double zValue = 0; byte[] xVarry = new byte[2]; byte[] yVarry = new byte[2]; byte[] zVarry = new byte[2]; ////}axis; // byte[] a = new byte[2]; // a[0] = 0xfb; // a[1] = 0xff; // Int16 b = BitConverter.ToInt16(a, 0); // double c = Convert.ToDouble(BitConverter.ToInt16(a, 0)); if (comClosing == true) return; //如果正在关闭,忽略操作,直接返回,尽快的完成串口监听线程的一次循环 try { handerListening = true;//设置标记,说明已经开始处理数据,一会儿要使用系统UI的。 int n = serialPort1.BytesToRead; byte[] buf = new byte[n]; recFrameNum ; recByteNum = n; serialPort1.Read(buf, 0, n); recBuffer.AddRange(buf); //对接收到的数据进行显示 this.Invoke((EventHandler)(delegate { labelRecByteCount.Text = Convert.ToString(recByteNum); labelRecFrameCount.Text = Convert.ToString(recFrameNum); if (checkBoxRecDisplay.Checked == true) { foreach (var temp in buf) { richTextBox1.Text = temp.ToString("X2") " "; } richTextBox1.AppendText("\r\n"); this.richTextBox1.ScrollToCaret(); } })); if(buf.Length >= 8) { if (buf[0] == ConfigPrama.FrameHeader && buf[7] == ConfigPrama.FrameTail) { //x轴 xVarry[1] = buf[1]; xVarry[0] = buf[2]; xValue = Convert.ToDouble(BitConverter.ToInt16( xVarry,0)); //y轴 yVarry[1] = buf[3]; yVarry[0] = buf[4]; yValue = Convert.ToDouble(BitConverter.ToInt16(yVarry, 0)); //轴 zVarry[1] = buf[5]; zVarry[0] = buf[6]; zValue = Convert.ToDouble(BitConverter.ToInt16(zVarry, 0)); this.Invoke((EventHandler)(delegate { paint_ax(xValue, yValue, zValue); })); } } else { } } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { handerListening = false; } } #endregion #region 清空接收区和接收记录 private void buttonClear_Click(object sender, EventArgs e) { richTextBox1.Text = ""; recFrameNum = 0; recByteNum = 0; labelRecByteCount.Text = "0"; labelRecFrameCount.Text = "0"; } #endregion private void buttonSave_Click(object sender, EventArgs e) { } #region 绘图接口 void paint_ax(double x,double y,double z) { // Define some variables int numberOfPointsInChart = 100; //int numberOfPointsAfterRemoval = 40; // Simulate adding new data points //int numberOfPointsAddedMin = 5; //int numberOfPointsAddedMax = 10; try { chart1.ResetAutoValues(); if (checkBoxX.Checked == true) { //chart1.Series["xAxis"].Points.AddY(x); chart1.Series["xAxis"].Points.AddXY(pointIndex, x); ; } if (checkBoxY.Checked == true) { chart1.Series["yAxis"].Points.AddXY(pointIndex,y); } if (checkBoxZ.Checked == true) { chart1.Series["zAxis"].Points.AddXY(pointIndex,z); } pointIndex ; // Keep a constant number of points by removing them from the left //if (chart1.Series[0].Points.Count > numberOfPointsInChart) //{ // // Remove data points on the left side // while (chart1.Series[0].Points.Count > numberOfPointsAfterRemoval) // { // chart1.Series[0].Points.RemoveAt(0); // //chart1.Series[1].Points.RemoveAt(0); // //chart1.Series[2].Points.RemoveAt(0); // } //} if(chart1.Series[0].Points.Count > numberOfPointsInChart) { axisChange = true; // Remove data points on the left side //while (chart1.Series[0].Points.Count > numberOfPointsAfterRemoval) //{ // chart1.Series[0].Points.RemoveAt(0); //} } // Adjust X axis scale if(axisChange) { chart1.ChartAreas["ChartArea1"].AxisX.Minimum = pointIndex 1 - numberOfPointsInChart; chart1.ChartAreas["ChartArea1"].AxisX.Maximum = chart1.ChartAreas["ChartArea1"].AxisX.Minimum numberOfPointsInChart; //chart1.ChartAreas["ChartArea1"].AxisY.Minimum = -100; } // Invalidate chart chart1.Invalidate(); // Set series chart type //chart1.Series["xAxis"].ChartType = SeriesChartType.Line; // Set point labels //chart1.Series["xAxis"].IsValueShownAsLabel = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion #region 清除绘图 private void buttonGclear_Click(object sender, EventArgs e) { chart1.Series["xAxis"].Points.Clear(); chart1.Series["yAxis"].Points.Clear(); chart1.Series["zAxis"].Points.Clear(); pointIndex = 0; chart1.ChartAreas["ChartArea1"].AxisX.Minimum = 0; chart1.ChartAreas["ChartArea1"].AxisX.Maximum = 100; axisChange = false; } #endregion private void comboBoxPort_SelectedIndexChanged(object sender, EventArgs e) { } private void chart1_Click(object sender, EventArgs e) { } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复